package com.lottery.im.module.service.ui;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.fondesa.recyclerviewdivider.RecyclerViewDivider;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.integral.ExchangeLog;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.module.service.adapter.ExchangeLogAdapter;
import com.lottery.im.module.service.presenter.ExchangeLogPresenter;
import com.lottery.im.module.service.view.ExchangeLogView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者：cyy
 * 时间：2018/9/20。
 */
public class ExchangeLogFragment extends BaseSwipeFragment<ExchangeLogPresenter>
    implements ExchangeLogView {
  @BindView(R.id.tv_title) TextView tvTitle;
  @BindView(R.id.recycleView) RecyclerView recycleView;
  @BindView(R.id.smartRefreshLayout) SmartRefreshLayout smartRefreshLayout;
  private ExchangeLogAdapter mAdapter;
  private List<ExchangeLog> mList = new ArrayList<>();

  private int startRow=0;//起始页面
  private int pageSize=10;//每页数量

  @Override protected int getLayoutResId() {
    return R.layout.fragment_exchangelog;
  }

  @Override protected void initPresenter() {
    presenter = new ExchangeLogPresenter(mActivity, this, this);
    presenter.init();
  }

  @Override public void initView() {
    tvTitle.setText("兑换记录");

    recycleView.setLayoutManager(new LinearLayoutManager(mActivity));
    RecyclerViewDivider.with(mActivity)
        .size((int) mActivity.getResources().getDimension(R.dimen.line_size))
        .hideLastDivider().build().addTo(recycleView);

    mAdapter = new ExchangeLogAdapter();
    recycleView.setAdapter(mAdapter);

    //state==0初次进来
    presenter.loadExchangeLogGetLimit(startRow, pageSize,0);

    //下拉刷新
    smartRefreshLayout.setOnRefreshListener(refreshLayout -> {
      startRow=0;
      presenter.loadExchangeLogGetLimit(startRow, pageSize,1);
      refreshLayout.finishRefresh();
    });

    //上拉加载更多
    smartRefreshLayout.setOnLoadMoreListener(refreshLayout -> {
      startRow+=pageSize;
      presenter.loadExchangeLogGetLimit(startRow,pageSize,2);
      refreshLayout.finishLoadMore();
    });
  }

  @Override public void loadExchangeLogGetLimitSuccess(List<ExchangeLog> list,int state) {
    if(state==0||state==1){//第一次进来或下拉刷新
      mAdapter.notifyList(list);
    }else{//上拉加载更多
      mAdapter.addList(list);
    }
  }

  @OnClick(R.id.iv_back) public void onViewClicked() {
    popBackStack();
  }
}
